package com.corgi.multi.core.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

/**
 * PostgresConfig
 *
 * @title: PostgresConfig
 * @description:
 * @author: dengmiao
 * @create: 2020-05-20 20:06
 **/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="postgresEntityManagerFactory",
        transactionManagerRef="postgresTransactionManager",
        basePackages= {"com.corgi.multi.modules.sys.read.domain"}) //设置Repository所在位置
public class PostgresConfig {

    @Value("${spring.jpa.properties.hibernate.postgres-dialect}")
    private String dialect;

    @Autowired
    private HibernateProperties hibernateProperties;

    @Autowired
    private JpaProperties jpaProperties;

    @Autowired
    @Qualifier("postgresDataSource")
    private DataSource postgresDataSource;

    @Bean(name = "postgresEntityManager")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return postgresEntityManagerFactory(builder).getObject().createEntityManager();
    }

    @Bean(name = "postgresEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean postgresEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(postgresDataSource)
                .properties(getVendorProperties())
                .packages("com.corgi.multi.modules.sys.read.domain.entity") //设置实体类所在位置
                .persistenceUnit("postgresPersistenceUnit")
                .build();
    }

    @Bean(name = "postgresTransactionManager")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(postgresEntityManagerFactory(builder).getObject());
    }

    private Map<String, Object> getVendorProperties() {
        Map<String, Object> ret = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
        ret.put("hibernate.dialect", dialect);
        return ret;
    }
}
